package com.qianseit.westore.activity.helpcentre;

import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.webkit.WebSettings.LayoutAlgorithm;
import android.webkit.WebView;

import com.qianseit.westore.BaseDoFragment;
import com.qianseit.westore.Run;
import com.qianseit.westore.http.JsonRequestBean;
import com.qianseit.westore.http.JsonTaskHandler;
import com.beautymiracle.androidclient.R;

public class HelpArticleFragment extends BaseDoFragment {
	
	private String articleId;
	private String title;
	private String mContent;
//	private TextView mTextView;
	private WebView mWebView;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Bundle b = getArguments();
		if (b != null) {
			title = b.getString("title");
			articleId = b.getString("article_id");
			mContent = b.getString(Run.EXTRA_DATA);
		}
		mActionBar.setTitle(title);
	}
	
	@Override
	public void init(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		rootView = inflater.inflate(R.layout.fragment_help_article, null);
//		mTextView = (TextView) rootView.findViewById(R.id.fragment_help_article_content);
		mWebView = (WebView) rootView.findViewById(R.id.fragment_help_article_content01);
		mWebView.getSettings().setUseWideViewPort(true);
		mWebView.getSettings().setLoadWithOverviewMode(true);
		if (!TextUtils.isEmpty(mContent)) {
			mWebView.setBackgroundColor(0x00000000);
			mWebView.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
			mWebView.loadDataWithBaseURL(null, mContent, "text/html", "utf8", null);
		} 
		//new JsonTask().execute(new GetArticleDetail());
	}
	
	private class GetArticleDetail implements JsonTaskHandler{

		@Override
		public void task_response(String json_str) {
			try {
				JSONObject all = new JSONObject(json_str);
				if(Run.checkRequestJson(mActivity, all)){
					JSONObject data = all.getJSONObject("data");
					JSONObject body = data.getJSONObject("bodys");
					mContent = body.optString("content");
					mWebView.setBackgroundColor(0x00000000);
					mWebView.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
					mWebView.loadDataWithBaseURL(null, mContent, "text/html", "UTF-8", null);
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}

		@Override
		public JsonRequestBean task_request() {
			JsonRequestBean req = new JsonRequestBean(Run.API_URL, "mobileapi.article.get_detail");
			req.addParams("article_id", articleId);
			return req;
		}
		
	}
}
